<template>
  <el-dialog
    title="脚本执行"
    :visible.sync="dialogVisible"
    width="60%"
    :before-close="handleClose"
  >
    <el-col :span="12">
      <div v-if="type===1">
        <el-divider content-position="left">参数配置</el-divider>
        <el-form label-width="120px" style="padding-right: 10px;">
          <el-form-item v-for="(value, key) in parameter" :key="key" :label="key">
            <el-input
              v-model="parameter[key]"
              placeholder="请输入内容"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="type===0">
        <el-upload
          ref="upload"
          class="upload-demo"
          drag
          :action="baseUploadUrl+'&id='+ruleForm.id"
          :auto-upload="false"
          :on-success="successUpload"
          :on-error="errorUpload"
          multiple
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击此处</em></div>
          <el-button style="margin-left: 10px;" size="small" type="success" @click.stop="submitUpload">开始上传</el-button>
        </el-upload>
      </div>
    </el-col>
    <el-col :span="12">
      <el-divider content-position="left">执行结果</el-divider>
      <el-card>
        <el-input v-model="msg" type="textarea" :rows="10" />
      </el-card>
    </el-col>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button v-if="type===1" type="primary" @click="runScript()">执行</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { runGroovy } from '@/api/groovy'
import { getToken, getTokenKey } from '@/utils/token'
export default {
  name: 'RunScript',
  data() {
    return {
      baseUploadUrl: window.SITE_CONFIG.baseUrl + '/groovy/runFileScript?' + getTokenKey() + '=' + getToken(),
      dialogVisible: false,
      msg: '',
      parameter: '',
      type: '',
      ruleForm: {
        id: '',
        paramValue: {}
      }
    }
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit()
    },
    successUpload(response, file, fileList) {
      if (response.code == 200) {
        this.$message.success('上传成功')
        this.msg = response.data
      } else {
        this.$message.error('上传失败')
      }
    },
    errorUpload(err, file, fileList) {
      this.$message.error('上传失败')
    },
    runScript() {
      debugger
      let valueJson
      if (this.parameter !== '') {
        valueJson = this.parameter
      } else {
        valueJson = {}
      }
      runGroovy({
        id: this.ruleForm.id,
        paramValue: valueJson
      }).then((r) => {
        this.msg = r
      })
    },
    init(row) {
      this.ruleForm.id = row.id
      if (row.parameter) {
        this.parameter = JSON.parse(row.parameter)
      }
      this.type = row.type
    },
    handleClose() {
      this.$parent.init()
      this.$parent.RunScriptVisible = false
    }
  }
}
</script>

<style scoped>
</style>
